﻿using UnityEngine;

public abstract class MoveableObject : BaseBehaviour
{
	private float endWayX;
	private MoveBehaviour moveBehaviour;	

	protected abstract void bindEndX(ref float endWayX);
	protected abstract void initMoveBehaviour(MoveBehaviour moveBehaviour);

	protected override void start()
	{
		bindFields();
		initMoveBehaviour();        
        
		base.start();
	}

	protected override void fixedUpdate(float deltaTime)
	{
		moveBehaviour.move(deltaTime);
	}

	protected override void update(float deltaTime)
	{
		moveBehaviour.update(deltaTime);
	}

	private void initMoveBehaviour()
	{
		moveBehaviour = new MoveBehaviour(this);

		initMoveBehaviour(moveBehaviour);
	}

	protected virtual void bindFields()
	{
		bindEndX(ref endWayX);
	}

	protected override void lateUpdate()
	{
		if (hasComeToEnd())
		{
			onComeToEndWay();
		}
	}

	private bool hasComeToEnd()
	{
		return transform.localPosition.x < endWayX;
	}

	protected virtual void onComeToEndWay()
	{

	}	
}
